<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .list{
            width: 800px;
            height: 500px;
            margin: auto;
        }
        .item{
           width: 100%;
           height: 100px;
           display: flex;
           align-items: center;
        }
        img{
            width: 90px;
            height: 90px;
        }
        .name,.spec,.price,.count,.sub-total{
            width: 200px;
            height: 90px;
            display: flex;
            flex-direction: column;
            flex-wrap: wrap;
        }
        .name span{
            width: 100%;
            color: #ccc;
            font-size: 10px;
        }
    </style>
</head>
<body>
<div class="list"></div>
<div class="total">
    <p>合计：<span class="amount"></span></p>
</div>
</body>
</html>
<script>
      const goodsList = [
        {
            id: '4001172',
            name: '杯子',
            price: '289.30',
            picture: 'https://pic1.zhimg.com/v2-6cc0298000328b10f7ab82144974c2bc_r.jpg',
            count:2,
            spec:{color:'白色'}
        },
        {
            id: '4001172',
            name: '枕头',
            price: '99.90',
            picture: 'https://pic1.zhimg.com/v2-6cc0298000328b10f7ab82144974c2bc_r.jpg',
            count:2,
            spec:{color:'黑色',size:'40cm*40cm'}
        },
        {
            id: '4001172',
            name: '床',
            price: '199.00',
            picture: 'https://pic1.zhimg.com/v2-6cc0298000328b10f7ab82144974c2bc_r.jpg',
            count:2,
            spec:{color:'青色',sum:'一大四小'}
        },
        {
            id: '4001172',
            name: '椅子',
            price: '399.00',
            picture: 'https://pic1.zhimg.com/v2-6cc0298000328b10f7ab82144974c2bc_r.jpg',
            count:2,
            spec:{color:'紫色',size:'小号'},
            gift:'50g茶叶,清洗球'
        },
    ]
    const list = document.querySelector('.list')
    list.innerHTML = goodsList.map(item => {
        let {name,price,picture,count,spec,gift} = item
        // 处理数据
        /** 
         * 把 spec 对象转成字符串，获取所有的属性值
         * 把 gift 对象进行切割，然后用 map 进行处理，然后 join 进行渲染
         * 注意： gift 为 undefined 不是所有商品都有赠品
         * **/
        const specStr = Object.values(spec).join('/')

        const giftStr = gift? gift.split(',').map(item =>`<span class="tag">【赠品】${item}</span>`).join('') :''

        return `<div class="item">
                      <img src='${picture}' alt="">
                      <p class="name">${name}${giftStr}</p>
                      <p class="spec">${specStr}</p>
                      <p class="price">${price}</p>
                      <p class="count">${count}</p>
                      <p class="sub-total">${(price * count).toFixed(2)}</p>
                    </div>`
    }).join('')
// toFixed(x)  保留x为小数
     document.querySelector('.amount').innerHTML = goodsList.reduce( (prev,item)=> prev + item.price * item.count ,0).toFixed(2)
</script>